/*
#ifndef _SELFCDEBUG
    #define _SELFCDEBUG
    #include <debug.h>
#endif
*/

// example
/*
int i;
STACK_INFO* pstack_info;
pstack_info=get_stack_2_string();
for(i=0; i < pstack_info->stacknum; i++) {
  msg_print(0054, tm_str_id, 0L, pstack_info->c_stack[i]);
}
*/
STACK_INFO* get_stack_2_string() {
    static STACK_INFO stack_info;
    stack_info.stacknum = backtrace(stack_info.stackbuffer, BUFSIZE);
    stack_info.c_stack = backtrace_symbols(stack_info.stackbuffer, BUFSIZE);
    return &stack_info;
}


// example
/*
#include "debug.h"

// 修改或清空文件 line:18
record_stack_2_file();
*/
int record_stack_2_file() {
  FILE* fp;
  int i;
  STACK_INFO* pstack_info;
  pstack_info = get_stack_2_string();
  fp = fopen("/opt/DPX/logs/tptmm.stack.info", "a");
  if (fp == NULL) return -1;
  for(i=0; i < pstack_info->stacknum; i++) {
    fputs(pstack_info->c_stack[i], fp);
    fputs("\n", fp);
    fputs("------------------------\n", fp);
  }
  fclose(fp);
  return 0;
}


// example
/*
#include<stdio.h>
#include<string.h>
int main(){
    unsigned char *str="hello";
    size_t len=strlen(str);
    char hex[len*2+1];
    memset(hex, 0, len*2+1);
    str_to_hex(str, hex, len);
    printf("%s\n", hex);
    return 0;
}
*/
void str_to_hex(unsigned char *str, char *hex, int len) {
    int i, j=0;
    for(i=0; i<len; i++){
        sprintf(hex+(j++)*2, "%02x", str[i]);
    }
}



